<?php

// @formatter:off
/**
 * @file tags.php
 * @author Alejandro Dario Simi
 * @date $Date: 2014-06-03 03:40:32 +0000 (Tue, 03 Jun 2014) $
 *
 * $Id: tags.php 112 2014-06-03 03:40:32Z daemonraco@gmail.com $
 * $URL: http://wcomix.googlecode.com/svn/trunk/frontview/actions/content/tags.php $
 */
// @formatter:on

class ActionControllerContentTags extends WCActionController {
	//
	// Public methods.
	public function check() {
		$check = parent::check();

		if($check == WC_HTTPERROR_OK) {
			global $wcProfile;

			if(!$wcProfile->allowedTo(WC_PERM_TAG_LIST)) {
				$check = WC_HTTPERROR_FORBIDDEN;

				global $errno;
				global $error;

				$errno = WC_HTTPERROR_FORBIDDEN;
				$error = "You're not allowed to list tags";
			}
		}

		return $check;
	}
	//
	// Protected methods.
	protected function subRun() {
		$ok = true;

		//
		// Loading global pointers.
		global $wcSkinConfig;
		global $wcGroup;

		WCViewManager::I()->invalidate(WCViewManager::KeyImage, 0);

		$page = isset($_REQUEST["page"]) ? $_REQUEST["page"] : 1;
		$minTagInPage = $wcSkinConfig->max_per_page->tags * ($page - 1);
		$maxTagInPage = $minTagInPage + $wcSkinConfig->max_per_page->tags - 1;

		$this->_manager->PATH = "All Tags (page {$page})";
		//
		// Selecting only tags in range.
		$tags = array();
		$tagCount = 0;
		$tagsFull = WCTagsHolder::I()->itemsByGroup($wcGroup);
		uasort($tagsFull, "WCTag::NameSort");
		foreach($tagsFull as $tag) {
			if($tag->ok() && ($tag->count > 0 || $tag->icount > 0)) {
				$tagCount++;
			} else {
				continue;
			}
			if($tagCount >= $minTagInPage && $tagCount < $maxTagInPage) {
				$tags[] = $tag;
			}
		}

		WCFVPager::GetAssignsIntoManager(ceil($tagCount / $wcSkinConfig->max_per_page->tags));
		//
		// Creating multi-array by prefix
		$lists = array();
		$ids = 1;
		foreach($tags as $tag) {
			$name = $tag->name;
			$prefix = strtoupper(isset($name[0]) ? $name[0] : "#");
			if($prefix < "A" || $prefix > "Z") {
				$prefix = "#";
			}
			$prefix = "{$prefix}";
			if(!isset($lists[$prefix])) {
				$lists[$prefix] = array(
					"prefix" => $prefix,
					"list" => array()
				);
			}

			$lists[$prefix]["list"][] = WCFVBoxes::GetTagPreviewAssigns($tag);
		}
		ksort($lists);

		$this->_manager->tags_lists = $lists;

		return $ok;
	}
}
